package com.itheima.service.impl;


import com.itheima.controller.CategoryController;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public void add(Category category) {

        //完善数据
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        //获取用户id
        Map<String, Object> map = ThreadLocalUtil.get();

        Integer id = (Integer) map.get("id");

        category.setCreateUser(id);


        categoryMapper.add(category);


    }

    @Override
    public List<Category> get(){

        Map<String, Object> map = ThreadLocalUtil.get();

        Integer userId = (Integer) map.get("id");

       return categoryMapper.get(userId);

    }

    @Override
    public Category findById(Integer id) {
        return categoryMapper.findById(id);
    }

    @Override
    public void update(Category category) {

        category.setUpdateTime(LocalDateTime.now());

        categoryMapper.update(category);


    }

    @Override
    public void delete(Integer id) {

        categoryMapper.delete(id);

    }


}
